﻿package mortal.game.scene3D.layer3D
{
    import Message.Public.*;
    import com.gengine.debug.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.utils.*;
    import mortal.game.rules.*;
    import mortal.game.scene3D.ai.singleAIs.autofight.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.map3D.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.utils.*;

    public class EntityDictionary extends Object
    {
        private var _map:Dictionary;
        private var _length:int;

        public function EntityDictionary(param1:Boolean = false)
        {
            this._map = new Dictionary(param1);
            return;
        }// end function

        public function get allEntitys() : Dictionary
        {
            return this._map;
        }// end function

        public function get length() : int
        {
            return this._length;
        }// end function

        public function addEntity(param1:SEntityId, param2:IEntity, param3:Boolean = true) : IEntity
        {
            this._map[this.entityToString(param1)] = param2;
            (this._length + 1);
            var _loc_4:* = GameStatistical;
            var _loc_5:* = GameStatistical.sceneObjNum + 1;
            _loc_4.sceneObjNum = _loc_5;
            return param2;
        }// end function

        public function removeEntity(param1:SEntityId, param2:Boolean = true) : IEntity
        {
            if (param1 == null)
            {
                return null;
            }
            var _loc_3:* = this.entityToString(param1);
            var _loc_4:* = this._map[_loc_3];
            if (_loc_4)
            {
                this._map[_loc_3] = null;
                (this._length - 1);
                var _loc_5:* = GameStatistical;
                var _loc_6:* = GameStatistical.sceneObjNum - 1;
                _loc_5.sceneObjNum = _loc_6;
                delete this._map[_loc_3];
            }
            return _loc_4;
        }// end function

        public function getEntityByString(param1:String) : IEntity
        {
            return this._map[param1];
        }// end function

        public function getEntity(param1:SEntityId) : IEntity
        {
            return this._map[this.entityToString(param1)];
        }// end function

        private function entityToString(param1:SEntityId) : String
        {
            return EntityUtil.toString(param1);
        }// end function

        private function getDistance(param1:DisplayObject, param2:DisplayObject) : Number
        {
            return Math.pow(param1.x - param2.x, 2) + Math.pow(param1.y - param2.y, 2);
        }// end function

        public function getEntityByRangle(param1:Rectangle, param2:int = 0, param3:Boolean = true) : Array
        {
            var _loc_5:IEntity = null;
            var _loc_4:* = new Array();
            for each (_loc_5 in this._map)
            {
                
                if (param3 == false || param1.contains(_loc_5.x2d, _loc_5.y2d))
                {
                    if (param2 == 0 && _loc_5.isDead == false)
                    {
                        _loc_4.push(_loc_5);
                        continue;
                    }
                    if (_loc_5.type == param2 && _loc_5.isDead == false)
                    {
                        _loc_4.push(_loc_5);
                    }
                }
            }
            return _loc_4;
        }// end function

        public function removeAll() : void
        {
            this._map = new Dictionary();
            this._length = 0;
            GameStatistical.sceneObjNum = 0;
            return;
        }// end function

        public function selectEntityByMonsterCode(param1:int) : IEntity
        {
            var _loc_4:IEntity = null;
            var _loc_2:* = this.getEntityByRangle(SceneRange.display);
            var _loc_3:int = 0;
            while (_loc_3 < _loc_2.length)
            {
                
                _loc_4 = _loc_2[_loc_3] as IEntity;
                if (_loc_4.entityInfo && _loc_4.entityInfo.entityInfo.code == param1)
                {
                    ThingUtil.selectEntity = _loc_4;
                    return _loc_4;
                }
                _loc_3++;
            }
            return null;
        }// end function

        public function selectEntity(param1:Array = null, param2:int = 20, param3:Boolean = false, param4:Array = null) : IEntity
        {
            var _loc_6:IEntity = null;
            if (ThingUtil.selectEntity)
            {
                if (isEntityFit(ThingUtil.selectEntity, param1, param2, param3, true) && isInDicPlayer(ThingUtil.selectEntity))
                {
                    return ThingUtil.selectEntity;
                }
            }
            if (param4 == null)
            {
                param4 = this.getEntityByRangle(SceneRange.display);
            }
            AutoFightBossSelectAI.instance.updateMyPoint();
            param4.sort(AutoFightBossSelectAI.instance.sortByDistance);
            var _loc_5:int = 0;
            while (_loc_5 < param4.length)
            {
                
                _loc_6 = param4[_loc_5] as IEntity;
                if (_loc_6 == null)
                {
                }
                else if (_loc_6 is PetPlayer)
                {
                }
                else if (_loc_6.entityInfo.isDisappear && RolePlayer.instance != _loc_6)
                {
                }
                else if (!isEntityFit(_loc_6, param1, param2, param3))
                {
                }
                else
                {
                    if (_loc_6 != RolePlayer.instance)
                    {
                        ThingUtil.selectEntity = _loc_6;
                    }
                    return _loc_6;
                }
                _loc_5++;
            }
            ThingUtil.selectEntity = null;
            return null;
        }// end function

        public function updateHeadContainer() : void
        {
            var _loc_1:IEntity = null;
            for each (_loc_1 in this._map)
            {
                
                _loc_1.updateHeadContainer();
            }
            return;
        }// end function

        public static function isEntityFit(param1:IEntity, param2:Array, param3:int, param4:Boolean, param5:Boolean = false, param6:Boolean = true) : Boolean
        {
            var _loc_8:MonsterPlayer = null;
            if (!param1 || !param1.entityInfo)
            {
                return false;
            }
            if (param6 && param1.entityInfo.isInHidden)
            {
                return false;
            }
            if (param1 is MonsterPlayer)
            {
                _loc_8 = param1 as MonsterPlayer;
                if (!_loc_8.entityInfo || _loc_8.entityInfo.entityInfo.life <= 0)
                {
                    return false;
                }
                if (!BossRule.isBossTypeCanSelect(_loc_8.bossInfo.type))
                {
                    return false;
                }
            }
            if (param2 != null && param2[param1.type] == false)
            {
                return false;
            }
            if (param4 && !param1.isDead)
            {
                return false;
            }
            if (!param4 && param1.isDead)
            {
                return false;
            }
            var _loc_7:* = EntityRelationUtil.isFriend(param1.entityInfo.entityInfo);
            if (param5)
            {
                if (param3 == EntityRelationUtil.FIREND)
                {
                    if (!_loc_7)
                    {
                        return false;
                    }
                }
                else if (_loc_7)
                {
                    return false;
                }
            }
            else if (param3 == EntityRelationUtil.FIREND)
            {
                if (!_loc_7)
                {
                    return false;
                }
            }
            else if (_loc_7)
            {
                return false;
            }
            return true;
        }// end function

        public static function isInDicPlayer(param1:IEntity) : Boolean
        {
            var _loc_2:* = param1.type;
            return _loc_2 == EntityType.Boss || _loc_2 == EntityType.Player || _loc_2 == EntityType.Pet;
        }// end function

    }
}
